<?php
defined ( 'BASEPATH' ) or exit ( 'No direct script access allowed' );
/**
 * 用户模型
 */
class Message_model extends CI_Model {
	public function __construct() {
		parent::__construct ();
	}
	
	/**
	 * 新增留言
	 */
	public function message_insert($adminid) {
		$data = array (
				'message_title' => $_POST ['title'],
				'message_content' => $_POST ['content'],
				'message_uid' => $adminid,
				'message_time' => time () 
		);
		
		$result = $this->db->insert ( 'pc_ddth_message', $data );
		
		return $result;
	}
	
	/**
	 * 查询所有留言
	 */
	public function message_list($id = FALSE, $message_uid = FALSE) {
		if ($id === FALSE && $message_uid === FALSE) {
			$query = $this->db->get_where ( 'pc_ddth_message', array (
					'reply_uid' => null 
			) );
			return $query->result_array ();
		}
		
		if ($message_uid) {
			$query = $this->db->get_where ( 'pc_ddth_message', array (
					'message_uid' => $message_uid 
			) );
			return $query->result_array ();
		}
		
		$query = $this->db->get_where ( 'pc_ddth_message', array (
				'id' => $id 
		) );
		return $query->row_array ();
	}
	
	// 回复
	public function reply($adminid) {
		$data = array (
				'reply_content' => $_POST ['reply'],
				'reply_time' => time (),
				'reply_uid' => $adminid, 
				'reply_status' => 1
		);
		
		$this->db->where ( 'id', $_POST ['id'] );
		$result = $this->db->update ( 'pc_ddth_message', $data );
		
		return $result;
	}
	
	// 修改回复状态
	public function reply_status() {
		$data = array ( 
			'read_status' => 1
		);
		
		$this->db->where ( 'id', $_GET ['id'] );
		$result = $this->db->update ( 'pc_ddth_message', $data );
		
		return $result;
	}
	
	/**
	 *
	 */
	public function out_list_all($page,$perpage,$user_id) {
		$sql = " select * from pc_ddth_message where message_uid='$user_id' order by id limit ".$perpage*($page-1).",$perpage ";
	
		$query = $this->db->query ( $sql );
	
		return $query->result_array ();
	}
	
	/**
	 *
	 */
	public function out_list_all_count($user_id) {
		$query = $this->db->get_where ( 'pc_ddth_message', array (
				'message_uid' => $user_id
		) );
	
		return count($query->result_array ());
	}
	
	// 未阅读留言数
	public function mail_num($user_id) {
		$query = $this->db->get_where ( 'pc_ddth_message', array (
				'message_uid' => $user_id,
				'reply_status' => 1,
				'read_status' => 0 
		) );
		
		return count ( $query->result_array () );
	}
}